<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SupplementaryFiles
 * @creation_date  04-07-2012
 * @author          Lisette
 */
class SupplementaryFilesController extends BaseController {

	/**
	 * Binds the 5 files from the form and then
	 * Save them into the server (file system)
	 */
	public function saveFS($filename, $path) {
		 
		$files = $this->bind($filename);
		return $this->insertFS($files, $path);
	}
	
	/**
	 * Save the 5 sup. files into the FS
	 */
	function insertFS($files, $path){
		//$library_path is defined in data.php
		$suplement="suplement";
		//echo $path.$suplement.DIRECTORY_SEPARATOR;
		$i=1;
		$return = 0;
		$error = array();
		 
		foreach ($files as $file) {
			 
			$move = move_uploaded_file($_FILES['form_supplementary_file'.$i]['tmp_name'], "$path/supplement/$file");
			if ($move == false) {
				$return = 0;
				$error[] = "Error! The supplementary file ".$_FILES['form_supplementary_file'.$i]['name']." has not been recorded.<br>";
			}
			if ($move == true){
				$return = 1;
				$message[] = "The supplementary file file ".$_FILES['form_supplementary_file'.$i]['name']." has been recorded.<br>";
			}
			++$i;
		}
		 
		$return_message = array($return, array("errors"=>$error, "message"=>$message));
		return $return_message;
	}
	
	protected function check(){
		$directives = '<BR><BR><U>Relevant directives in php.ini:</U>';
		$directives .= '<BR>file_uploads: '.ini_get ('file_uploads').' (required 1)';
		$directives .= '<BR>upload_max_filesize: '.ini_get ('upload_max_filesize').' (recommended 60M)';
		$directives .= '<BR>post_max_size: '.ini_get ('post_max_size').' (recommended 60M)';
		$directives .= '<BR>max_input_time: '.ini_get ('max_input_time').' (recommended 60)';
		 
		 
	
	
		if (isset($_FILES['form_supplementary_file']['error']) && $_FILES['form_supplementary_file']['error'] > 0) {
		  
			if ($_FILES['form_supplementary_file']['error'] == 1) die("The uploaded file size exceeds the <b>upload_max_filesize</b> directive in <b>php.ini</b>.$directives");
			if ($_FILES['form_supplementary_file']['error'] == 3) die("The uploaded file was only partially uploaded.$directives");
			if ($_FILES['form_supplementary_file']['error'] == 6) die("Missing a temporary folder.$directives");
			if ($_FILES['form_supplementary_file']['error'] == 7) die("Failed to write file to disk.$directives");
			if ($_FILES['form_supplementary_file']['error'] == 8) die("File upload stopped by extension.$directives");
		}
		 
	}
	/**
	 *
	 * @return array
	 */
	private function bind($filename) {
		$files = array();
	
		for ($i = 1; $i <= 5; $i++) {
			if (isset($_FILES['form_supplementary_file' . $i]) && is_uploaded_file($_FILES['form_supplementary_file' . $i]['tmp_name']) && preg_match('/\.(php|php4|php3|htm|html)$/i', $_FILES['form_supplementary_file' . $i]['name']) == 0) {
				$supplement_filename = sprintf("%05d", intval($filename)) . $_FILES['form_supplementary_file' . $i]['name'];
				$files[] = $supplement_filename;
			}
		}
		return $files;
	}
    

}

?>
